package com.example.business;

import com.example.data.model.SignRequest;
import com.example.data.model.User;
import com.example.utils.DemoException;
import com.example.utils.Encrypt;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * Created by hldev on 17-2-8.
 */

@Service
public class SignService {

    @Autowired
    private UserService userService;

    /**
     * 登录
     */
    public User signin(SignRequest signRequest) {
        if (userService.findByUserName(signRequest.getUserName()) != null) {
            User user = userService.findByUserName(signRequest.getUserName());
            if (Encrypt.encryptStrWithMD5(signRequest.getPassWord() + signRequest.getUserName()).equals(user.getPassWord())) {
                return userService.findByUserName(signRequest.getUserName());
            } else {
                throw new DemoException("密码错误");
            }
        }
        throw new DemoException("账号不存在");
    }
}
